﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using System.Data.SqlClient;
using System.Collections;

namespace WindowsFormsApplication2
{
    public partial class frmCustomer : Form
    {
        private SqlConnection conn;
        ArrayList delNumbers = new ArrayList();
        public frmCustomer(SqlConnection co)
        {
            conn = co;
            InitializeComponent();
        }      

        private void frmRegCustomer_Load(object sender, EventArgs e)
        {

            dataGridViewCustomer.DataSource = DBTools.selecQryTmp("Select *from Customers");
            FormatGrid();
        }


        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewCustomer.Columns[colname].HeaderText = caption;
            dataGridViewCustomer.Columns[colname].DisplayIndex = index;
            dataGridViewCustomer.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid()
        {
            dataGridViewCustomer.AllowUserToOrderColumns = true;
            dataGridViewCustomer.AllowUserToAddRows = false;
            dataGridViewCustomer.AllowUserToDeleteRows = false;
            dataGridViewCustomer.ReadOnly = true;
            dataGridViewCustomer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewCustomer.MultiSelect = true;

            FormatColumn("CustomerId", "Харилцагчийн код", 0);
            FormatColumn("CustomerFirstName", "Харилцагчийн овог", 1);
            FormatColumn("CustomerLastName", "Харилцагчийн нэр", 2);
            FormatColumn("CustomerRd", "Регистрийн дугаар", 3);
            FormatColumn("CustomerAddress", "Харилцагчийн хаяг", 4);
            FormatColumn("CustomerPhone", "Харилцагчийн утас", 5);
            FormatColumn("XTolologchId", "Худалдааны төлөөлөгчийн код", 5);
            dataGridViewCustomer.Columns["CustomerPass"].Visible = false;
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridViewCustomer.DataSource = DBTools.selecQryTmp("Select *from Customers");
        }

        private void btnRegAdd_Click_1(object sender, EventArgs e)
        {
            frmRegCustomer frm = new frmRegCustomer(conn);
            frm.Show();
            dataGridViewCustomer.DataSource = DBTools.selecQryTmp("Select *from Customers");
        }

        private void btnRegDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Та үнэхээр устгах уу?", "Анхаар", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    int i = 0;
                    Customer cls = new Customer();

                    i = dataGridViewCustomer.CurrentRow.Index;
                    DataRowView drv = dataGridViewCustomer.CurrentRow.DataBoundItem as DataRowView;

                    if (drv != null)
                    {
                        string byComputerrec = drv[0].ToString();

                        Customer.DelFunction(byComputerrec);
                        MessageBox.Show("Амжилттай устгалаа");
                        dataGridViewCustomer.DataSource = DBTools.selecQryTmp("Select *from Customers");
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message, "Алдаа", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnRegEdit_Click(object sender, EventArgs e)
        {
            
        }

        private void grboxReg_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridViewCustomer_DoubleClick(object sender, EventArgs e)
        {
            int i = 0;
            Customer cls1 = new Customer();

            i = dataGridViewCustomer.CurrentRow.Index;
            DataRowView drv = dataGridViewCustomer.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string byCode = drv[0].ToString();

                cls1 = Customer.getbyRec(byCode);
                Form EA = new frmRegCustomer(cls1);
                EA.ShowDialog();
                dataGridViewCustomer.DataSource = DBTools.selecQryTmp("Select *from Customers");
            }
        }

        
    }
}
